;;;the way you do that is by producing a language at that level of detail
 
(define deriv
  (lambda (f) ;;输入一个f
    (lambda (x) ;;返回一个procedure
      (/ (- (f (+ x dx))
            (f x))
         dx))))
 
;;为什么求导容易，积分困难
;;reduction rule
 
(define (deriv exp var)
  (cond ((constant? exp var) 0)
        ((same-var? exp var) 1)
        ((sum? exp) (make-sum (deriv (a1 exp) var)
                              (deriv (a2 exp) var)))
        ((product? exp) (make-sum (make-product (m1 exp) (deriv (m2 exp) var))
                                 (make-product (deriv (m1 exp) var) (m2 exp))))
        ... ))
 
;;;在这里我们用到了大量的“按愿望思维”
;;;you have realize there's a lot of wishful thinking here
 
;;;表达式如何表示？使用内建的lisp的元素
 
(define (constant? exp var)
  (and (atom? exp)
       (not (eq? exp var))))
 
(define (same-var? exp var)
  (and (atom? exp)
       (eq? exp var)))
 
;;I don't want to look inside this, this stuff anymore.
;;There are primitive maybe.
 
(define (sum? exp)
  (and (not (atom? exp))
       (eq (car exp) '+)))
 
 
 
(define (make-sum a1 a2)
  (list '+ a1 a2))
 
(define a1 cadr)
(define a2 caddr)
 
(define (product? exp)
  (and (not (atom? exp))
       (eq? (car exp) '*)))
 
(define (make-product m1 m2)
  (list '* m1 m2))
 
(define m1 cadr)
(define m2 caddr)
 
 
;;a*x*x + b*x + c
(define foo
  '(+ (* a (* x x))
      (+ (* b x)
         c)))
 
;;求导为
;;2*a*x + b
 
(deriv foo 'x)
;;得到一个没有化简的表达式
 
;;abstraction barrier
 
 
;;;deriv rules
;;;==================================
;;;constant? same-var?
;;;sum? make-sum a1 a2 ... 其他过程接口
;;;==================================
;;;representation list structure
 
 
(define (make-sum a1 a2)
  (cond ((and (number? a1) (number? a2))
         (+ a1 a2))
        ((and (number? a1) (= a1 0))
         a2)
        ((and (number? a2) (= a2 0))
         a1)
        (else (list '+ a1 a2))))

